package com.aoip.telemetry.telemetry.receive.handler;

import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.constant.TelemetryConstants;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.aoip.telemetry.telemetry.entity.equipment.StOnlineStatusEntity;
import com.aoip.telemetry.telemetry.receive.call.controller.QuerySetOrderController;
import com.aoip.telemetry.telemetry.receive.session.Session;
import com.aoip.telemetry.telemetry.receive.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * 绑定测站地址，便于召测功能
 *
 * @author chenbai
 * @version 1.0
 * @date 2021/10/19 15:36
 */
@Component
@Slf4j
@ChannelHandler.Sharable
public class BindStationAddressHandle extends ChannelInboundHandlerAdapter {


    private String stCodeIndexRange;

    private BindStationAddressHandle() {
    }

    private static BindStationAddressHandle bindStationAddressHandle;

    @PostConstruct
    public void init() {
        bindStationAddressHandle = this;
    }

    public BindStationAddressHandle(String stCodeIndexRange) {
        this.stCodeIndexRange = stCodeIndexRange;
    }

    @Resource
    private RedisCache redisCache;
    @Resource
    private QuerySetOrderController querySetOrderController;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (!StringUtils.hasText(stCodeIndexRange) || !stCodeIndexRange.contains(",")) {
            return;
        }
        String[] range = stCodeIndexRange.split(",");
        int begin = Integer.valueOf(range[0]);
        int end = Integer.parseInt(range[1]);
        String s = msg.toString();
        if (s.length() < end) {
            return;
        }
        String stCode = s.substring(begin, end);
        Session session = SessionFactory.getSession();
        Channel channel = ctx.channel();
        session.bind(channel, stCode);
        channel.localAddress();
        InetSocketAddress inSocket = (InetSocketAddress) channel.localAddress();
        session.setAttribute(channel, "stCode", stCode);
        session.setAttribute(channel, "protocolCommunicate", TelemetryConstants.TCP);
        session.setAttribute(channel, "inSocket", inSocket);
//        保存测站在线信息到redis
        StOnlineStatusEntity entity = new StOnlineStatusEntity();
        entity.setStCode(stCode);
        entity.setOnlineStatus(TelemetryConstants.ONLINE);
        entity.setOnlineTime(LocalDateTime.now());
        entity.setCommunicateIpPort(inSocket.getPort());
        String key = CacheKeyEnum.STATION_ONLINE_STATUS + ":" + stCode;
        bindStationAddressHandle.redisCache.deleteObject(key);
        bindStationAddressHandle.redisCache.setCacheObject(key, entity, 10, TimeUnit.MINUTES);

        bindStationAddressHandle.querySetOrderController.autoSend(stCode);
    }
}
